package com.lrkj.back.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.ChineseToEngLetterUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.cbons.mamisay.parent.exception.BusinessException;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/eng")
public class ToEngController extends BaseController{
	
	@RequestMapping("/cn2en.do")
	@ResponseBody
	public Map<Object,Object> cn2en(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		String prefix = request.getParameter("prefix");
		try {
			String chinseMsg = request.getParameter("msg");
			String dictCode = getMathStr(prefix)+ ChineseToEngLetterUtil.getAllFirstLetter(chinseMsg).toUpperCase();
			rtnMap.put("status", "true");
			rtnMap.put("msg", dictCode);
		} catch (Exception e) {
			rtnMap.put("status", "false");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	
	 public static String getMathStr(String prefix)throws BusinessException{
		 if(prefix!=null && !"".equals(prefix)){
			  return "";
		 }else{
			  StringBuffer sb = new StringBuffer("");
			  String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			  for(int i= 0;i<8 ;i++){
				  char a = chars.charAt((int)(Math.random() * 26));
				  sb.append(a);
			  }
			  sb.append("_");
			  return sb.toString();
		 }
	 }
}